﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sunboxsoft.Common;
using System.Collections.Generic;

namespace Sunboxsoft.ASMS.Server.Domain.UnitTest
{
    [TestClass]
    public class CreateServiceUnitTest
    {
        private Service.CreateService _service;
        public CreateServiceUnitTest()
        {
            Logger.InitByLogDirectory(System.Configuration.ConfigurationManager.AppSettings["SYS.LogDirectory"]);
            Domain.Library.Load();
            _service = new Service.CreateService();
        }

        [TestMethod]
        public void TestCreateAutomobile()
        {
            Entity.EAutomobile automobile = new Entity.EAutomobile();
            automobile.CustomerId = new Service.QueryService().QueryCustomerPagedList(null, 0, 10).Data.Items[0].Id;
            automobile.BoughtDate = DateTime.Now;
            automobile.Brand = "brand";
            automobile.EIN = "EIN";
            automobile.EngineType = "EngineType";
            automobile.InsuranceCompany = "InsuranceCompany";
            automobile.InsuranceExpirationDate = DateTime.Now;
            automobile.InsuranceNumber = "InsuranceNumber";
            automobile.LPN = "LPN";
            automobile.Maker = "Maker";
            automobile.Model = "Model";
            automobile.Owner = "Owner";
            automobile.OwnerTelephone = "OwnerTelephone";
            automobile.VIN = "VIN";
            automobile.Series = "Series";
            ServiceResult result = _service.CreateAutomobile(automobile);
            Assert.IsTrue(result.IsSuccess);
        }

        [TestMethod]
        public void TestCreateServiceCategory()
        {
            Entity.EServiceCategory category = new Entity.EServiceCategory();

            DateTime now = DateTime.Now;
            category.Name = string.Format("类别{0:X00000000}", now.Ticks);
            category.Code = string.Format("C{0:X00000000}", now.Ticks);

            Assert.IsTrue(new Service.CreateService().CreateServiceCategory(category).IsSuccess);
        }

        [TestMethod]
        public void TestCreateServiceItem()
        {
            //PagedResult<Entity.EServiceCategory> pagedResult = new Service.QueryService().QueryServiceCategoryPagedList(0, 999);

            //int randIndex = new Random().Next(0, pagedResult.Data.Items.Count - 1);
            //DateTime now = DateTime.Now;
            //Entity.EServiceItem item = new Entity.EServiceItem();
            //item.CategoryId = pagedResult.Data.Items[randIndex].Id;
            //item.Name = string.Format("项目{0:X00000000}", now.Ticks);
            //item.Code = string.Format("I{0:X00000000}", now.Ticks);
            //item.Price = 100;

            //Assert.IsTrue(new Service.CreateService().CreateServiceItem(item).IsSuccess);
        }

        [TestMethod]
        public void TestCreateBookingBill()
        {
            //Entity.EBookingBill bill = new Entity.EBookingBill();
            //bill.CustomerId = new Service.QueryService().QueryCustomerPagedList(null, 0, 99).Data.Items[0].Id;
            //bill.AutomobileId = new Service.QueryService().QueryAutomobileList(bill.CustomerId).Data.Items[0].Id;
            //bill.BusinessId = new Guid("{647AE906-43F8-447B-A7AB-90DCD09E41CC}");
            //bill.StationId = new Guid("{0EB976A2-5A5A-4E67-A434-CD0E70E69AEA}");
            //bill.TargetDate = DateTime.Now.AddDays(10);

            //Entity.EBookingService selectedService1 = new Entity.EBookingService();
            //selectedService1.ServiceCategoryId = new Service.QueryService().QueryServiceCategoryPagedList(0, 99).Data.Items[0].Id;

            //Entity.EBookingService selectedService2 = new Entity.EBookingService();
            //selectedService2.ServiceCategoryId = new Service.QueryService().QueryServiceCategoryPagedList(0, 99).Data.Items[1].Id;

            //ServiceResult result = _service.CreateBookingBill(bill, new Entity.EBookingService[]{
            //    selectedService1,
            //    selectedService2
            //});
            //Assert.IsTrue(result.IsSuccess);
        }

        [TestMethod]
        public void TestCreatePayItem()
        {
            Entity.EPayItem item = new Entity.EPayItem();
            DateTime now = DateTime.Now;

            item.Name = string.Format("项目{0:X00000000}", now.Ticks);
            item.BusinessId = null;
            item.StationId = null;

            int rd = new Random().Next(0,2);
            if( rd == 1)
            {
                item.Type = Constant.PayItemTypes.Payment;
                item.Amount = 10.02m + rd * 3.84m;
            }
            else
            {
                item.Type = Constant.PayItemTypes.Discount;
                item.Amount = 10.02m + rd * 5.56m;
            }
            item.Description = string.Format("description for PI{0:X00000000}", now.Ticks);

            ServiceResult result = _service.CreatePayItem(item);
            Assert.IsTrue(result.IsSuccess);
        }

        [TestMethod]
        public void TestCreateMaterial()
        {
            Entity.EMaterial material = new Entity.EMaterial();
            DateTime now = DateTime.Now;
            material.Name = string.Format("商品{0:X00000000}", now.Ticks);
            material.Code = string.Format("M{0:X00000000}", now.Ticks);
            material.Category = Constant.MasterDataCodes.MaterialCategory + "01";
            material.Brand = Constant.MasterDataCodes.MaterialBrand + "01";
            material.Unit = Constant.MasterDataCodes.MaterialUnit + "01";
            material.Price = 100;
            material.Cost = 80;
            material.QuickCode = string.Format("{0:X00000000}", now.Ticks);
            material.Barcode = string.Format("{0:X00000000}", now.Ticks);
            material.SalesCommission = 0.01m;
            material.SalesCommissionType = Constant.CommissionTypes.Rate;
            //material.SalesCommissionAmount = (material.Price - material.Cost) * material.SalesCommission;

            SingleResult<Entity.EMaterial> result = _service.CreateMaterial(material);
            Assert.IsTrue(result.IsSuccess);
        }

        [TestMethod]
        public void TestCreateWarehouse()
        {
            Entity.EWarehouse warehouse = new Entity.EWarehouse();
            DateTime now = DateTime.Now;
            warehouse.Name = string.Format("仓库{0:X00000000}", now.Ticks);
            warehouse.Code = string.Format("WH{0:X00000000}", now.Ticks);
            ServiceResult result = _service.CreateWarehouse(warehouse);
            Assert.IsTrue(result.IsSuccess);
        }


        [TestMethod]
        public void TestCreateStockIncreaseBill()
        {
            //Entity.EStockIncreaseBill bill = new Entity.EStockIncreaseBill();
            //bill.WarehouseId = new Service.QueryService().QueryWarehousePagedList(null, null, Constant.CommonStatus.ALL, 0, 999).Data.Items[0].Id;

            //PagedResult<View.VMaterial> mResult = new Service.QueryService().QueryMaterialPagedList(null, null, null, null, Constant.CommonStatus.ALL, 0, 99);
            //List<Entity.EStockIncreaseBillItem> itemList = new List<Entity.EStockIncreaseBillItem>();
            //for (int index = 0; index < mResult.Data.Items.Count; index++)
            //{
            //    Entity.EStockIncreaseBillItem item = new Entity.EStockIncreaseBillItem();
            //    item.MaterialId = mResult.Data.Items[index].Id;
            //    item.SalesMode = Constant.SalesModes.Sell;
            //    item.Cost = index * 1.2m;
            //    item.Count = index * index + 101 + index * 3.3m;
            //    itemList.Add(item);
            //}
            //bill.Type = Constant.StockChangeTypes.IncreaseOther;
            //ServiceResult result = _service.CreateStockIncreaseBill(bill, itemList.ToArray());
            //Assert.IsTrue(result.IsSuccess);
        }

        [TestMethod]
        public void TestCreateSupplier()
        {
            Entity.ESupplier supplier = new Entity.ESupplier();
            supplier.Name = string.Format("供应商{0:X00000000}", DateTime.Now.Ticks);
            supplier.ContactName = "王先生";
            supplier.ContactTelephone = "1333333333";
            supplier.Telephone = "77945455";
            ServiceResult result = _service.CreateSupplier(supplier);
            Assert.IsTrue(result.IsSuccess);
        }
    }
}
